/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "BluetoothSerial.h"
#include "Base64.h"

REGISTER_PLUGIN_CLASS(BluetoothSerial)
bool BluetoothSerial::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "list") {
        m_cbc = cbc;
        m_vecId.clear();
        if(m_pRet != NULL) {
            cJSON_Delete(m_pRet);
        }
        m_pRet = cJSON_CreateArray();
        return executeArkTs("/Ble/startBLEScan", 0, "1", "BluetoothSerial", cbc);
    }
    if(action == "connect") {
        m_cbc = cbc;
        string address = cJSON_GetArrayItem(args, 0)->valuestring;
        return executeArkTs("/Ble/connect", 0, address.c_str(), "BluetoothSerial", cbc);
    }
    if(action == "disconnect") {
        m_cbc = cbc;
        return executeArkTs("/Ble/disconnect", 0, "", "BluetoothSerial", cbc);
    }
    if(action == "write") {
        m_cbc = cbc;
        string data = cJSON_GetArrayItem(args, 0)->valuestring;
        return executeArkTs("/Ble/write", 0, data.c_str(), "BluetoothSerial", cbc);
    }
    if(action == "enable") {
        cbc.success();
    }
    if(action == "setDeviceDiscoveredListener") {
        cbc.success();
    }
    if(action == "clearDeviceDiscoveredListener") {
        cbc.success();
    }
    if(action == "stopDiscoverUnpaired") {
        return executeArkTs("/Ble/stopBLEScan", 0, "1", "BluetoothSerial", cbc);
    }
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    return true;
}
bool BluetoothSerial::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(content == "startBLEScan") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            for(int i=0; i<count; i++) {
                string strIdName = cJSON_GetArrayItem(json,i)->valuestring;
                if(strIdName.find(",") != string::npos) {
                    string id = strIdName.substr(0, strIdName.find(","));
                    string name = strIdName.substr(strIdName.find(",")+1);
                    vector<string>::iterator it =  find(m_vecId.begin(), m_vecId.end(), id);
                    if(it != m_vecId.end())
                        continue;
                    m_vecId.push_back(id);
                    cJSON* pObject = cJSON_CreateObject();
                    cJSON_AddStringToObject(pObject, "id", id.c_str());
                    cJSON_AddStringToObject(pObject, "address", id.c_str());
                    cJSON_AddStringToObject(pObject, "name", name.c_str());
                    cJSON_AddItemToArray(m_pRet, pObject);
                }
            }
            PluginResult pluginResult(PluginResult::Status::OK, m_pRet);
            pluginResult.setKeepCallback(true);
            m_cbc.sendPluginResult(pluginResult);
        }
    }
    
    if(content == "connect") {
        if(json != NULL && json->type == cJSON_Array) {
            string strError = "";
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                strError = cJSON_GetArrayItem(json,0)->valuestring;
            }
            if(strError == "OK") {
                PluginResult pluginResult(PluginResult::Status::OK);
                pluginResult.setKeepCallback(false);
                m_cbc.sendPluginResult(pluginResult);
            } else {
                m_cbc.error(strError);
            }
        }
    }
    
    if(content == "disconnect") {
        m_cbc.success();
    }
    
    if(content == "write") {
        m_cbc.success();
    }
    return true;
}